#include "parser.h"
#include "core.h"

using namespace std;

namespace newsclient {

        bool Parser::parse() {
                string arg1, arg2;
          //      istringstream iss(in_str, istringstream::in);
                getline(cin, input);
				
				// state independant commands
                if(input == "quit") {
                  core->quit();
                  return false;
                }
                else if(input == "help" || input == "commands") {
                  core->printCommands();
                  return true;
                }
                // state depenadant commands
                else {
	                switch(core->getState()) {
	                case 0:			// At root
	                        if(input == "list") {
								core->listNewsgroups();
								return true;
	                        }
	                        else if(input == "add") {
	                        	cout << "Enter newsgroup name: ";
	                        	getline(cin, arg1);
	                          core->addNewsgroup(arg1);
	                            return true;
	                        }
	                        else if(input == "delete") {
	                        		cout << "Enter a newsgroup ID: ";
	                                getline(cin, arg1);
	                                core->deleteNewsgroup(arg1);
	                                return true;
	                        }
	                        break;
	                case 1:
	                        if(input == "list") {
	                                core->listArticles();
	                                return true;
	                        }
	                        else if(input == "add") {
								cout << "Enter author name: ";
    	                    	getline(cin, arg1);
    	                   		cout << "Enter article title: ";
	                       		getline(cin, arg2);
	                            core->addArticle(arg1, arg2);
	                            return true;
	                        }
	                        else if(input == "delete") {
			                        cout << "Enter an article ID: ";
	                                getline(cin, arg1);
	                                core->deleteArticle(arg1);
	                                return true;
	                        }
	                        else if(input == "get") {
	                                getline(cin, arg1);
	                                core->getArticle(arg1);
	                                return true;
	                        }
	                        break;
	                  }
                }
                cout << "Unknown command\n";
                return true;
        }
}
